package com.yjs.app.trade.service.service.business.invoice.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yjs.app.trade.entity.base.util.Page;
import com.yjs.app.trade.entity.bean.DispatchInvoice;
import com.yjs.app.trade.service.mapper.business.invoice.InvoiceMapper;
import com.yjs.app.trade.service.service.business.invoice.InvoiceService;
import com.yjs.business.ecps.order.model.InvoiceInfo;
import com.yjs.business.ecps.order.service.IInvoiceInfoService;
import com.yjs.core.common.OperationResult;

@Service("inoviceService")
public class InoviceServiceImpl implements InvoiceService {

	@Autowired
	InvoiceMapper invoiceMapper;
	@Autowired
	IInvoiceInfoService iInvoiceInfoService;
	@Override
	public Object findThirdPartyInvoice() {
		return null;
	}

	@Override
	public Map<String, Object> findPlatFormInovice(Map<String, Object> map) {
		Map<String, Object> inovice = invoiceMapper.findPlatFormInovice(map);
		if(inovice==null){
			Map<String, Object> result=new HashMap<String, Object>();
			result.put("fpje", "--");
			result.put("kprq", "--");
			result.put("fphm", "--");
			result.put("fpdm", "--");
			result.put("sphy", "--");
			result.put("kphy", "--");
			result.put("fpzt", "--");
			result.put("InvoiceInfoId", "--");
			result.put("bz", "--");
			return result;
		}
		Date date = (Date) inovice.get("kprq");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String kprq = format.format(date);
		inovice.put("kprq", kprq);	
		return inovice;
	}

	@Override
	public OperationResult confirmTheReceiptOfInvoice(String invoiceInfoId) {
		InvoiceInfo findInvoiceInfo = invoiceMapper.findInvoiceInfo(invoiceInfoId);
		OperationResult result = null;
		try {
			result = iInvoiceInfoService.confirmmemo(findInvoiceInfo);
		} catch (Exception e) {
			result = new OperationResult();
			result.setResultCode(2);
			result.setResultMessage(e.getMessage());
			return result;
		}
		return result;
	}

	@Override
	public OperationResult rejectInvoice(String invoiceInfoId,String refuseReason) {
		InvoiceInfo findInvoiceInfo = invoiceMapper.findInvoiceInfo(invoiceInfoId);
		findInvoiceInfo.setRefuseReason(refuseReason);
		OperationResult result = null;
		try {
			result = iInvoiceInfoService.refuse(findInvoiceInfo);
		} catch (Exception e) {
			result = new OperationResult();
			result.setResultCode(2);
			result.setResultMessage(e.getMessage());
			return result;
		}
		return result;
	}

	@Override
	public Page<?> findInvoice(Page<DispatchInvoice> page, DispatchInvoice dispatchInvoice) {
		dispatchInvoice.setPage(page);
		List<Map<String, Object>> list = invoiceMapper.findInvoice(dispatchInvoice);
		page.setMaps(list);
		return page;
	}

	@Override
	public List<Map<String, Object>> findProductByInvoice(String invoiceInfoId) {
		List<Map<String, Object>> list = invoiceMapper.findProductByInvoice(invoiceInfoId);
		return list;
	}

	@Override
	public Map<String, Object> findInvoiceNum(String invoiceInfoId) {
		
		return invoiceMapper.findInvoiceNum(invoiceInfoId);
	}

	

}
